#!/usr/bin/env ruby
# frozen_string_literal: true

# minimize-input: trim each cohort's input to no greater than the longest output's decode length

require "json"

warn "[+] pass: minimize-input"

count = 0
$stdin.each_line do |line|
  result = JSON.parse line, symbolize_names: true

  max_ndecoded = result[:outputs].map { |o| o[:ndecoded] }.max

  # If the maximum ndecoded is 0, then all are 0 and we should skip
  # this cohort entirely.
  # In effect, this is probably identical to filter-all-failure.
  if max_ndecoded.zero?
    count += 1
    next
  end

  # input is hex formatted, so the trimmed length is max_ndecoded * 2
  result[:input] = result[:input][0, max_ndecoded * 2]

  $stdout.puts result.to_json
end

warn "[+] pass: minimize-input done: #{count} filtered"
